﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;

using ITViet.Web.App_Code;
using ITViet.Domains;
using ITViet.Services;
using DevExpress.Web.ASPxGridView;
using DevExpress.Web.ASPxEditors;
using DevExpress.Web.ASPxClasses;
using DevExpress.Web.ASPxDataView;

namespace ITViet.Web.Admin
{
  public partial class Video : AdminBasePage
  {
    #region Properties

    private string m_innerError = string.Empty;
    private string ERR0001 = "ERROR|";

    internal override int RequiredPermission
    {
      get
      {
        return (int)FunctionList.Video;
      }
    }

    private string innerError
    {
      set
      {
        m_innerError = ERR0001 + value;
      }
      get
      {
        return m_innerError;
      }
    }

    private string Path
    {
      get
      {
        return hfPath.Value;
      }
    }
    #endregion

    #region Methods
    internal override void SetPermission()
    {
      if (!CanModify) btnAdd.Visible = btnSave.Visible = btnSaveState.Visible = false;
      if (!CanDelete) btnDelete.Visible = ckbAll.Visible = false;
    }

    protected void Page_Load(object sender, EventArgs e)
    {
      if (!IsPostBack || dvVideo.IsCallback)
        LoadVideo();
    }

    private void LoadVideo()
    {
      TList<Multimedia> vlist = new MultimediaService().GetAllVideo();
      foreach (Multimedia item in vlist)
      {
        if (!string.IsNullOrEmpty(item.Path))
        {
          string host = "http://" + Request.Url.Host + ":" + Request.Url.Port + Request.ApplicationPath;
          string file = host + "/Upload/Video/" + item.Path;
          item.Path = Utils.EmbedObject(file, "155", "155", item.Id.ToString());
        }
        if (item.Status == "A")
          item.Status = "checked='checked'";
        else
          item.Status = string.Empty;
      }
      dvVideo.DataSource = vlist;
      dvVideo.DataBind();
    }

    private void Insert()
    {
      if (string.IsNullOrEmpty(txtName.Text.Trim()))
      {
        innerError = string.Format(Resources.Messages.ERR0007, Resources.Strings.Name);
        return;
      }

      string name = txtName.Text.Trim();
      Multimedia md = new Multimedia();
      md.Name = name;
      md.Path = this.Path;
      md.Status = VisibleStatus.Disable;
      md.Type = MultimediaType.Video;
      md.PostedDate = DateTime.Now;
      md.PostedBy = CurrentUser.Username;
      try
      {
        new MultimediaService().Save(md);
        LoadVideo();
      }
      catch (Exception ex)
      {
        Utils.DeleteFile(MapPath(FolderDirector.video + md.Path));
        WriteLog("Video.aspx::Save Video", ex);
        innerError = Resources.Messages.ERR0004;
      }
    }

    private void Delete()
    {
      if (hfListId.Value == "")
        return;
      try
      {
        MultimediaService ms = new MultimediaService();
        TList<Multimedia> list = ms.GetVideoByList(hfListId.Value);
        foreach (Multimedia item in list)
        {
          Utils.DeleteFile(MapPath(FolderDirector.video + item.Path));
        }
        ms.DeleteByIdList(hfListId.Value);
        LoadVideo();
      }
      catch (Exception ex)
      {
        WriteLog("Video.aspx::Delete Video", ex);
        innerError = Resources.Messages.ERR0060;
      }
    }

    private void UpdateState()
    {
      if (hfListId.Value == "")
        return;
      try
      {
        //string[] arrid = hfListId.Value.Split(';');
        //new MultimediaService().SaveStatusByIdList(arrid[0],VisibleStatus.Active);
        //new MultimediaService().SaveStatusByIdList(arrid[1], VisibleStatus.Disable);
        int Id = 0;
        int.TryParse(hfListId.Value, out Id);
        new MultimediaService().SetVideoOnHome(Id);
        LoadVideo();
      }
      catch (Exception ex)
      {
        WriteLog("Video.aspx::UpdateState Video", ex);
        innerError = Resources.Messages.ERR0004;
      }
    }
    #endregion

    #region Events
    protected void dvVideo_CustomCallback(object source, DevExpress.Web.ASPxClasses.CallbackEventArgsBase e)
    {
      if (e.Parameter.StartsWith("save"))
      {
        Insert();
        return;
      }
      if (e.Parameter.StartsWith("delete"))
      {
        Delete();
        return;
      }
      if (e.Parameter.StartsWith("state"))
      {
        UpdateState();
        return;
      }
    }

    protected void dvVideo_CustomJSProperties(object sender, DevExpress.Web.ASPxClasses.CustomJSPropertiesEventArgs e)
    {
      e.Properties["cpInnerError"] = innerError;
    }

    protected void dvVideo_DataBound(object sender, EventArgs e)
    {

    }

    protected void uploader_FileUploadComplete(object sender, DevExpress.Web.ASPxUploadControl.FileUploadCompleteEventArgs e)
    {
      if (e.UploadedFile == null)
      {
        innerError = string.Format(Resources.Messages.ERR0062, Resources.Strings.Name);
        return;
      }
      string filename = e.UploadedFile.FileName;
      string type = filename.Substring(filename.LastIndexOf('.') + 1);
      if (type.ToLower() != "swf" && type.ToLower() != "flv")
      {
        e.IsValid = false;
        e.ErrorText = Resources.Messages.ERR0021;
        return;
      }
      string result = Utils.SavePostedFile(e.UploadedFile, MapPath(FolderDirector.video));
      if (innerError.IndexOf("ERROR|") != -1)
      {
        e.IsValid = false;
      }
      result = result.Substring(result.LastIndexOf(@"\") + 1);
      e.CallbackData = result;
    }

    protected void ctrlCallback_Callback(object source, DevExpress.Web.ASPxCallback.CallbackEventArgs e)
    {

    }
    #endregion
  }
}
